import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_project/add_publish/pic_video_common_utils.dart';
import 'package:flutter_project/add_publish/pic_video_content_view.dart';
import 'package:flutter_project/add_publish/video_pic_content_view.dart';
import 'package:flutter_project/common/utils/screen_size_utils.dart';
import 'package:flutter_project/config/app_bar_view.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import '../generated/l10n.dart';
import '../home/home_video_details_model.dart';

class PublishAddMainPage extends StatefulWidget {
  final bool isFromDetail; //是否来自详情页面
  final VideoDetailsRespModel? itemDetailModel;
  final int type;

  const PublishAddMainPage(
      {this.isFromDetail = false,
      this.itemDetailModel,
      this.type = 1,
      super.key});

  @override
  State<PublishAddMainPage> createState() => _PublishAddMainPageState();
}

class _PublishAddMainPageState extends State<PublishAddMainPage>
    with SingleTickerProviderStateMixin {
  late TabController importTabController;
  List<String> tabList = ['图片生成', '视频生成'];
  double tabFontSize = 16.0;
  ValueNotifier<bool> isShow = ValueNotifier<bool>(false);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        CommonAppWidget.navAppBar(
            bgColor: Colors.white,
            title: S.of(context).create,
            childWidget: Column(
              children: [
                SizedBox(
                  height: PicVideoCommonUtils.tabHeight,
                  child: TabBar(
                    tabs: getTabList(),
                    controller: importTabController,
                    // labelColor: ThemeColor.black3D3D3DColor,
                    // unselectedLabelColor: ThemeColor.grayA0A0A0Color,
                    indicatorSize: TabBarIndicatorSize.label,
                    // indicatorColor: ThemeColor.appColor,
                    unselectedLabelStyle: TextStyle(
                      fontSize: tabFontSize,
                    ),
                    // 未选择样式
                    labelStyle: TextStyle(
                        fontSize: tabFontSize,
                        fontWeight: FontWeight.w500), // 选择的样式
                  ),
                ),
                Expanded(
                  child: TabBarView(controller: importTabController, children: [
                    PicVideoContentView(
                      itemDetailModel: widget.itemDetailModel,
                      isFromDetail: widget.isFromDetail,
                    ),
                    VideoPicContentView(
                      itemDetailModel: widget.itemDetailModel,
                      isFromDetails: widget.isFromDetail,
                    ),
                  ]),
                ),
              ],
            )),
        Positioned(
          child: Visibility(
            visible: Platform.isAndroid,
            child: ValueListenableBuilder(
                valueListenable: isShow,
                builder: (BuildContext context, bool value, Widget? child) {
                  return Visibility(
                    visible: value,
                    child: GestureDetector(
                      onTap: () {
                        isShow.value = false;
                      },
                      onPanDown: (res) {
                        isShow.value = false;
                      },
                      child: Container(
                        width: ScreenSizeUtils.deviceScreenWidth(context),
                        height: ScreenSizeUtils.deviceScreenHeight(context),
                        color: const Color.fromRGBO(0, 0, 0, 0.2),
                        child: Column(
                          children: [
                            Container(
                              padding: EdgeInsets.only(
                                  left: 30,
                                  top: ScreenSizeUtils.statusBarHeight(context),
                                  bottom: 20),
                              width: ScreenSizeUtils.deviceScreenWidth(context),
                              // height: 90,
                              color: Colors.white,
                              child: const Text(
                                '相机/相册权限说明\n允许星河画语使用您的相机/相册，用于导入图片/视频进行后续编辑处理',
                                style: TextStyle(fontSize: 16),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  );
                }),
          ),
        ),
      ],
    );
  }

  //组装tab
  List<Widget> getTabList() {
    List<Widget> list =
        List.generate(tabList.length, (index) => Tab(text: tabList[index]));
    return list;
  }

  @override
  void initState() {
    super.initState();
    tabList = [
      S.of(Get.context!).imageGeneration,
      S.of(Get.context!).videoGeneration
    ];
    importTabController = TabController(length: tabList.length, vsync: this);
    if (widget.isFromDetail) {
      importTabController.index = widget.type;
    }

    isOpenAlbumPermission().then((value) {
      isShow.value = value;
    });
  }

  //相册权限
  Future<bool> isOpenAlbumPermission() async {
    if (Platform.isAndroid) {
      final androidInfo = await DeviceInfoPlugin().androidInfo;
      if (androidInfo.version.sdkInt <= 32) {
        var status = Permission.storage.status;
        if (await status.isGranted) {
          return false;
        }
      }
    } else {
      var status = Permission.photos.status;
      if (await status.isGranted) {
        return false;
      }
    }

    return true;
  }

  @override
  void dispose() {
    importTabController.dispose();
    super.dispose();
  }
}
